package com.flyduck.security.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p></p>
 *
 * @author flyduck 2021/7/2
 * @version V1.0
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify by user: flyduck 2021/7/2
 * @modify by reason:{方法名}:{原因}
 **/
@RestController
@RequestMapping("user")
@Api("用户控制器")
public class UserController {

    @GetMapping
    @ApiOperation("获取用户信息")
    //@PreAuthorize("isAuthenticated()")
    public String get() {
        return "hello";
    }

    @GetMapping("userName")
    @ApiOperation("获取用户名")
    //@PreAuthorize("hasRole('USER')")
    public String getUserName() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userName = authentication.getName();
        return userName;
    }
}
